package com.gocoolmall.member.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Configuration
public class GoCoolMallFeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                System.out.println("requestInterceptor线程"+Thread.currentThread().getId());
                if (attributes != null){
                    HttpServletRequest request = attributes.getRequest(); //老师请求
                    if (request != null){
                        //同步请求头数据，Cookie
                        String session = request.getHeader("Cookie");
                        //给新请求同步了老请求的Cookie
                        requestTemplate.header("Cookie",session);
                        System.out.println("feign远程之前先进行requestInterceptors.apply");
                    }
                }

            }
        };
    }
}
